class ErrorsController < ApplicationController
  # GET /errors
  # GET /errors.xml
  def index
    p params
    case params['_method']
      when "GET"
      when "POST"
        return create
      else
    end
    
    if !logged_in?
      return redirect_to(  :controller => 'home', :action => 'index' )
    end
    
    @matching = nil
    @title = 'all errors'
    conditions = []
    values =[]
    if params[:browser] && params[:browser] != 'all'
      conditions.push("browser = ?")
      values.push(params[:browser])
      @title = params[:browser]+' errors'
    end
    case params[:group]
    when 'new', 'accepted','resolved','closed'
      conditions.push("status = ?")
      values.push(params[:group])
      if @title == 'all errors'
        @title = 'all '+params[:group]+' errors'
      else
        @title =  params[:group]+' '+@title
      end
      
    when 'week'
      conditions.push("created_at >= ?")
      d = DateTime.new - 7
      values.push(d)
      @title += ' in the last week'
    when 'month'
      conditions.push("created_at >= ?")
      d = DateTime.new << 1
      values.push(d)
      @title += ' in the last month'
    end
    
    if params[:compare]
      conditions.push("compare like ?")
      values.push(params[:compare].gsub('\\','\\\\\\\\')+'%') #don't ask me why this works exactly
      @matching = params[:compare]
      @title += ' matching:'
    end
    

    
    if conditions.length > 0
      
      condition = [conditions.join(' AND ')]+values
      p condition
      @errors = current_user.application_errors.find(:all, :conditions => condition)
    else 
      @errors = current_user.application_errors
    end
    
    

    respond_to do |format|
      format.html # index.rhtml
    end
    
    store_location
  end

  # GET /errors/1
  # GET /errors/1.xml
  def show
    @error = ApplicationError.find(params[:id])
    return render_text('go away') if @error.user !=  current_user

    respond_to do |format|
      format.html # show.rhtml
    end
  rescue
    redirect_to(:controller => 'home', :action => 'myerrors')
  end

  # GET /errors/new
  def new
    @error = ApplicationError.new
  end

  # GET /errors/1;edit
  def edit
    @error = ApplicationError.find(params[:id])
  end

  # POST /errors
  # POST /errors.xml
  def create
    return render_text( "alert('DamnIT, no application key!');") if(!params[:user_crypted_key])
    
    
    params[:error][:user_crypted_key]=params[:user_crypted_key]
    
    params[:error][:multirequest]=params[:_mutlirequest] if params[:_mutlirequest]
    
    if params[:id]
      @error = ApplicationError.find(params[:id])
      unless params[:user_crypted_key] == @error.user.crypted_key
        return render_text("alert('you suck');")
      end
      
      @error.multirequest = params[:error][:multirequest] if params[:error][:multirequest]
      params[:error].each{|key, value|
        next unless ApplicationError.column_names.include?(key.to_s)
        current = @error.send(key.to_s)
        @error.send(key.to_s+'=', current+value)
      }
    else
      @error = ApplicationError.new(params[:error])
    end

    if request.env && request.env['HTTP_USER_AGENT']
      @error.user_agent = request.env['HTTP_USER_AGENT']
    end


    respond_to do |format|
      @error.save

      if @error.errors.empty?
        flash[:notice] = 'ApplicationError was successfully created.'
        format.html { redirect_to error_url(@error) }
        format.xml  { head :created, :location => error_url(@error) }
        format.json { 
              render_text( 
                callback+'('+ ({:status => 201, :error => error_url(@error), :id => @error.id}.to_json() ) +')'
              )
        }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @error.errors.to_xml }
        format.json { 
              render_text( "alert('DamnIT, bad application key!');")
        }
      end
    end
  end



  # DELETE /errors/1
  # DELETE /errors/1.xml
  def destroy
    @error = ApplicationError.find(params[:id])
    return render_text('go away') if @error.user !=  current_user
    @error.destroy
    respond_to do |format|
      format.html { redirect_back_or_default :controller => 'home', :action => 'myerrors' }
    end
  end
  
  
  def callback
    return params[:callback] if(params[:callback])
    return 'jesterCallback'
  end
  
  
  def update_status
    @error = ApplicationError.find(params[:id])
    
    return render_text('go away') if @error.user !=  current_user
    
    @error.multirequest = 'true' #get rid of this eventually
    
    if params[:status] == 'delete'
      @error.destroy
      render :nothing => true
    else
      @error.status = params[:status]
      @error.save
      render :partial => 'errors/status'
    end
    
  end
  
  
  
end
